package com.test.todo.databse;

/**
 * 
 * @author
 * 
 * TodoDbAdapter class provides the functionality to query, create and update the todos.
 */

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import com.test.todo.model.Todo;

public class TodoDbAdapter {

    // Database fields
    public static final String KEY_ROWID = "_id";
    public static final String KEY_CATEGORY = "category";
    public static final String KEY_SUMMARY = "summary";
    public static final String KEY_DESCRIPTION = "description";
    private static final String DATABASE_TABLE = "todo";
    private Context context;
    private SQLiteDatabase database;
    private TodoDatabaseHelper dbHelper;

    public TodoDbAdapter(Context context) {

        this.context = context;
    }

    //opens the database via the TodoDatabaseHelper class
    public TodoDbAdapter open() throws SQLException {

        dbHelper = new TodoDatabaseHelper(context);
        database = dbHelper.getWritableDatabase();
        return this;
    }

    public void close() {

        dbHelper.close();
    }

    /**
         * Create a new todo If the todo is successfully created return the new
         * rowId for that note, otherwise return a -1 to indicate failure.
         */

    public long createTodo(Todo item) {

        ContentValues initialValues = createContentValues(item);

        return database.insert(DATABASE_TABLE, null, initialValues);
    }

    /**
         * Update the todo
         */

    public boolean updateTodo(long rowId, Todo item) {

        ContentValues updateValues = createContentValues(item);

        return database.update(DATABASE_TABLE, updateValues, KEY_ROWID + "=" + rowId, null) > 0;
    }

    /**
         * Deletes todo
         */

    public boolean deleteTodo(long rowId) {

        return database.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }

    /**
         * Return a Cursor over the list of all todo in the database
         * 
         * @return Cursor over all notes
         */

    public Cursor fetchAllTodos() {

        return database.query(DATABASE_TABLE, new String[] {
                KEY_ROWID, KEY_CATEGORY, KEY_SUMMARY, KEY_DESCRIPTION
        }, null, null, null, null, null);
    }

    /**
         * Return a Cursor positioned at the defined todo
         */

    public Cursor fetchTodo(long rowId) throws SQLException {

        Cursor mCursor = database.query(true, DATABASE_TABLE, new String[] {
                KEY_ROWID, KEY_CATEGORY, KEY_SUMMARY, KEY_DESCRIPTION
        }, KEY_ROWID + "=" + rowId, null, null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    private ContentValues createContentValues(Todo items) {

        ContentValues values = new ContentValues();
        values.put(KEY_CATEGORY, items.category);
        values.put(KEY_SUMMARY, items.summary);
        values.put(KEY_DESCRIPTION, items.description);
        return values;
    }
}
